{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Pandas基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "       SaleID    name   regDate  model  brand  bodyType  fuelType  gearbox  \\\n0      200000  133777  20000501   67.0      0       1.0       0.0      0.0   \n1      200001   61206  19950211   19.0      6       2.0       0.0      0.0   \n2      200002   67829  20090606    5.0      5       4.0       0.0      0.0   \n3      200003    8892  20020601   22.0      9       1.0       0.0      0.0   \n4      200004   76998  20030301   46.0      6       0.0       NaN      0.0   \n...       ...     ...       ...    ...    ...       ...       ...      ...   \n49995  249995  111443  20041005    4.0      4       0.0       NaN      1.0   \n49996  249996  152834  20130409   65.0      1       0.0       0.0      0.0   \n49997  249997  132531  20041211    4.0      4       0.0       0.0      1.0   \n49998  249998  143405  20020702   40.0      1       4.0       0.0      1.0   \n49999  249999   78202  20090708   32.0      8       1.0       0.0      0.0   \n\n       power  kilometer  ...       v_5       v_6       v_7       v_8  \\\n0        101       15.0  ...  0.236520  0.000241  0.105319  0.046233   \n1         73        6.0  ...  0.261518  0.000000  0.120323  0.046784   \n2        120        5.0  ...  0.261691  0.090836  0.000000  0.079655   \n3         58       15.0  ...  0.236050  0.101777  0.098950  0.026830   \n4        116       15.0  ...  0.257000  0.000000  0.066732  0.057771   \n...      ...        ...  ...       ...       ...       ...       ...   \n49995    150       15.0  ...  0.263668  0.000292  0.141804  0.076393   \n49996    179        4.0  ...  0.255310  0.000991  0.155868  0.108425   \n49997    147       12.5  ...  0.262933  0.000318  0.141872  0.071968   \n49998    176       15.0  ...  0.282106  0.000023  0.067483  0.067526   \n49999      0        3.0  ...  0.231449  0.103947  0.096027  0.062328   \n\n            v_9      v_10      v_11      v_12      v_13      v_14  \n0      0.094522  3.619512 -0.280607 -2.019761  0.978828  0.803322  \n1      0.035385  2.997376 -1.406705 -1.020884 -1.349990 -0.200542  \n2      0.073586 -3.951084 -0.433467  0.918964  1.634604  1.027173  \n3      0.096614 -2.846788  2.800267 -2.524610  1.076819  0.461610  \n4      0.068852  2.839010 -1.659801 -0.924142  0.199423  0.451014  \n...         ...       ...       ...       ...       ...       ...  \n49995  0.039272  2.072901 -2.531869  1.716978 -1.063437  0.326587  \n49996  0.067841  1.358504 -3.290295  4.269809  0.140524  0.556221  \n49997  0.042966  2.165658 -2.417885  1.370612 -1.073133  0.270602  \n49998  0.009006  2.030114 -2.939244  0.569078 -1.718245  0.316379  \n49999  0.110180 -3.689090  2.032376  0.109157  2.202828  0.847469  \n\n[50000 rows x 30 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>SaleID</th>\n      <th>name</th>\n      <th>regDate</th>\n      <th>model</th>\n      <th>brand</th>\n      <th>bodyType</th>\n      <th>fuelType</th>\n      <th>gearbox</th>\n      <th>power</th>\n      <th>kilometer</th>\n      <th>...</th>\n      <th>v_5</th>\n      <th>v_6</th>\n      <th>v_7</th>\n      <th>v_8</th>\n      <th>v_9</th>\n      <th>v_10</th>\n      <th>v_11</th>\n      <th>v_12</th>\n      <th>v_13</th>\n      <th>v_14</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>200000</td>\n      <td>133777</td>\n      <td>20000501</td>\n      <td>67.0</td>\n      <td>0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>101</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.236520</td>\n      <td>0.000241</td>\n      <td>0.105319</td>\n      <td>0.046233</td>\n      <td>0.094522</td>\n      <td>3.619512</td>\n      <td>-0.280607</td>\n      <td>-2.019761</td>\n      <td>0.978828</td>\n      <td>0.803322</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>200001</td>\n      <td>61206</td>\n      <td>19950211</td>\n      <td>19.0</td>\n      <td>6</td>\n      <td>2.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>73</td>\n      <td>6.0</td>\n      <td>...</td>\n      <td>0.261518</td>\n      <td>0.000000</td>\n      <td>0.120323</td>\n      <td>0.046784</td>\n      <td>0.035385</td>\n      <td>2.997376</td>\n      <td>-1.406705</td>\n      <td>-1.020884</td>\n      <td>-1.349990</td>\n      <td>-0.200542</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>200002</td>\n      <td>67829</td>\n      <td>20090606</td>\n      <td>5.0</td>\n      <td>5</td>\n      <td>4.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>120</td>\n      <td>5.0</td>\n      <td>...</td>\n      <td>0.261691</td>\n      <td>0.090836</td>\n      <td>0.000000</td>\n      <td>0.079655</td>\n      <td>0.073586</td>\n      <td>-3.951084</td>\n      <td>-0.433467</td>\n      <td>0.918964</td>\n      <td>1.634604</td>\n      <td>1.027173</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>200003</td>\n      <td>8892</td>\n      <td>20020601</td>\n      <td>22.0</td>\n      <td>9</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>58</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.236050</td>\n      <td>0.101777</td>\n      <td>0.098950</td>\n      <td>0.026830</td>\n      <td>0.096614</td>\n      <td>-2.846788</td>\n      <td>2.800267</td>\n      <td>-2.524610</td>\n      <td>1.076819</td>\n      <td>0.461610</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>200004</td>\n      <td>76998</td>\n      <td>20030301</td>\n      <td>46.0</td>\n      <td>6</td>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>116</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.257000</td>\n      <td>0.000000</td>\n      <td>0.066732</td>\n      <td>0.057771</td>\n      <td>0.068852</td>\n      <td>2.839010</td>\n      <td>-1.659801</td>\n      <td>-0.924142</td>\n      <td>0.199423</td>\n      <td>0.451014</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>49995</th>\n      <td>249995</td>\n      <td>111443</td>\n      <td>20041005</td>\n      <td>4.0</td>\n      <td>4</td>\n      <td>0.0</td>\n      <td>NaN</td>\n      <td>1.0</td>\n      <td>150</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.263668</td>\n      <td>0.000292</td>\n      <td>0.141804</td>\n      <td>0.076393</td>\n      <td>0.039272</td>\n      <td>2.072901</td>\n      <td>-2.531869</td>\n      <td>1.716978</td>\n      <td>-1.063437</td>\n      <td>0.326587</td>\n    </tr>\n    <tr>\n      <th>49996</th>\n      <td>249996</td>\n      <td>152834</td>\n      <td>20130409</td>\n      <td>65.0</td>\n      <td>1</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>179</td>\n      <td>4.0</td>\n      <td>...</td>\n      <td>0.255310</td>\n      <td>0.000991</td>\n      <td>0.155868</td>\n      <td>0.108425</td>\n      <td>0.067841</td>\n      <td>1.358504</td>\n      <td>-3.290295</td>\n      <td>4.269809</td>\n      <td>0.140524</td>\n      <td>0.556221</td>\n    </tr>\n    <tr>\n      <th>49997</th>\n      <td>249997</td>\n      <td>132531</td>\n      <td>20041211</td>\n      <td>4.0</td>\n      <td>4</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>147</td>\n      <td>12.5</td>\n      <td>...</td>\n      <td>0.262933</td>\n      <td>0.000318</td>\n      <td>0.141872</td>\n      <td>0.071968</td>\n      <td>0.042966</td>\n      <td>2.165658</td>\n      <td>-2.417885</td>\n      <td>1.370612</td>\n      <td>-1.073133</td>\n      <td>0.270602</td>\n    </tr>\n    <tr>\n      <th>49998</th>\n      <td>249998</td>\n      <td>143405</td>\n      <td>20020702</td>\n      <td>40.0</td>\n      <td>1</td>\n      <td>4.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>176</td>\n      <td>15.0</td>\n      <td>...</td>\n      <td>0.282106</td>\n      <td>0.000023</td>\n      <td>0.067483</td>\n      <td>0.067526</td>\n      <td>0.009006</td>\n      <td>2.030114</td>\n      <td>-2.939244</td>\n      <td>0.569078</td>\n      <td>-1.718245</td>\n      <td>0.316379</td>\n    </tr>\n    <tr>\n      <th>49999</th>\n      <td>249999</td>\n      <td>78202</td>\n      <td>20090708</td>\n      <td>32.0</td>\n      <td>8</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>3.0</td>\n      <td>...</td>\n      <td>0.231449</td>\n      <td>0.103947</td>\n      <td>0.096027</td>\n      <td>0.062328</td>\n      <td>0.110180</td>\n      <td>-3.689090</td>\n      <td>2.032376</td>\n      <td>0.109157</td>\n      <td>2.202828</td>\n      <td>0.847469</td>\n    </tr>\n  </tbody>\n</table>\n<p>50000 rows × 30 columns</p>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "df_csv = pd.read_csv('../二手车交易价格预测/data/used_car_testB_20200421.csv',\n",
    "                     sep=' ',)  #看api可以发现spe默认是','，但是我的数据是以' '分割的\n",
    "df_csv"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "两种基本数据结构（Series、DataFrame），目测很重要。但是教程中似乎少说了一个Index，但会放在第三章详细讨论（是我太年轻😂）\n",
    "\n",
    "下面这两种方式分别得到的是`DataFrame`和`Series`对象\n",
    "\n",
    "十分有必要把这两种的参数形式强调一下，**要得到`DataFrame`传入的应该是`list`类型**！！！"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "0        200000\n1        200001\n2        200002\n3        200003\n4        200004\n          ...  \n49995    249995\n49996    249996\n49997    249997\n49998    249998\n49999    249999\nName: SaleID, Length: 50000, dtype: int64"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#注意这里的细节，参数是[col_list]形式\n",
    "df = df_csv[['SaleID','name']]\n",
    "se = df_csv['SaleID']\n",
    "df\n",
    "se\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "似乎python风格的代码没有类似getter和setter的用法，都是直接获取属性：\n",
    "\n",
    "但是在我看了源码后，发现还是自己太年轻，我看到了`@property`的注解，并且`values`这种看似属性，其实人家是方法，有点意思啊python\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[200000, 133777],\n       [200001,  61206],\n       [200002,  67829],\n       ...,\n       [249997, 132531],\n       [249998, 143405],\n       [249999,  78202]], dtype=int64)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series：\n",
    "se.values  # 这个应该对应的是data参数，此外还有index dtype name shape等属性，直接看源码好吧\n",
    "df.columns  # 这是series类型没有的属性\n",
    "\n",
    "df.values\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 汇总函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 50000 entries, 0 to 49999\n",
      "Data columns (total 30 columns):\n",
      " #   Column             Non-Null Count  Dtype  \n",
      "---  ------             --------------  -----  \n",
      " 0   SaleID             50000 non-null  int64  \n",
      " 1   name               50000 non-null  int64  \n",
      " 2   regDate            50000 non-null  int64  \n",
      " 3   model              50000 non-null  float64\n",
      " 4   brand              50000 non-null  int64  \n",
      " 5   bodyType           48496 non-null  float64\n",
      " 6   fuelType           47076 non-null  float64\n",
      " 7   gearbox            48032 non-null  float64\n",
      " 8   power              50000 non-null  int64  \n",
      " 9   kilometer          50000 non-null  float64\n",
      " 10  notRepairedDamage  50000 non-null  object \n",
      " 11  regionCode         50000 non-null  int64  \n",
      " 12  seller             50000 non-null  int64  \n",
      " 13  offerType          50000 non-null  int64  \n",
      " 14  creatDate          50000 non-null  int64  \n",
      " 15  v_0                50000 non-null  float64\n",
      " 16  v_1                50000 non-null  float64\n",
      " 17  v_2                50000 non-null  float64\n",
      " 18  v_3                50000 non-null  float64\n",
      " 19  v_4                50000 non-null  float64\n",
      " 20  v_5                50000 non-null  float64\n",
      " 21  v_6                50000 non-null  float64\n",
      " 22  v_7                50000 non-null  float64\n",
      " 23  v_8                50000 non-null  float64\n",
      " 24  v_9                50000 non-null  float64\n",
      " 25  v_10               50000 non-null  float64\n",
      " 26  v_11               50000 non-null  float64\n",
      " 27  v_12               50000 non-null  float64\n",
      " 28  v_13               50000 non-null  float64\n",
      " 29  v_14               50000 non-null  float64\n",
      "dtypes: float64(20), int64(9), object(1)\n",
      "memory usage: 11.4+ MB\n"
     ]
    },
    {
     "data": {
      "text/plain": "              SaleID           name       regDate        model         brand  \\\ncount   50000.000000   50000.000000  5.000000e+04  50000.00000  50000.000000   \nmean   224999.500000   68505.606100  2.003401e+07     47.64948      8.087140   \nstd     14433.901067   61032.124271  5.351615e+04     49.90741      7.899648   \nmin    200000.000000       1.000000  1.991000e+07      0.00000      0.000000   \n25%    212499.750000   11315.000000  1.999100e+07     11.00000      1.000000   \n50%    224999.500000   52215.000000  2.003091e+07     30.00000      6.000000   \n75%    237499.250000  118710.750000  2.007110e+07     66.00000     13.000000   \nmax    249999.000000  196808.000000  2.015121e+07    246.00000     39.000000   \n\n           bodyType      fuelType       gearbox         power     kilometer  \\\ncount  48496.000000  47076.000000  48032.000000  50000.000000  50000.000000   \nmean       1.793736      0.376498      0.226953    119.766960     12.598260   \nstd        1.764970      0.549281      0.418866    206.313348      3.912519   \nmin        0.000000      0.000000      0.000000      0.000000      0.500000   \n25%        0.000000      0.000000      0.000000     75.000000     12.500000   \n50%        1.000000      0.000000      0.000000    110.000000     15.000000   \n75%        3.000000      1.000000      0.000000    150.000000     15.000000   \nmax        7.000000      6.000000      1.000000  19211.000000     15.000000   \n\n       ...           v_5           v_6           v_7           v_8  \\\ncount  ...  50000.000000  50000.000000  50000.000000  50000.000000   \nmean   ...      0.248147      0.044624      0.124693      0.058198   \nstd    ...      0.045836      0.051664      0.201440      0.029171   \nmin    ...      0.000000      0.000000      0.000000      0.000000   \n25%    ...      0.243436      0.000035      0.062519      0.035413   \n50%    ...      0.257818      0.000801      0.095880      0.056804   \n75%    ...      0.265263      0.101654      0.125470      0.079387   \nmax    ...      0.291176      0.153403      1.411559      0.157458   \n\n                v_9          v_10          v_11          v_12          v_13  \\\ncount  50000.000000  50000.000000  50000.000000  50000.000000  50000.000000   \nmean       0.062113      0.019633      0.002759      0.004342      0.004570   \nstd        0.035723      3.764095      3.289523      2.515912      1.287194   \nmin        0.000000     -9.119719     -5.662163     -8.291868     -4.157649   \n25%        0.033880     -3.675196     -1.963928     -1.865406     -1.048722   \n50%        0.058749      1.632134     -0.375537     -0.138943     -0.036352   \n75%        0.087624      2.846205      1.263451      1.775632      0.945239   \nmax        0.211304     12.177864     18.789496     13.384828      5.635374   \n\n               v_14  \ncount  50000.000000  \nmean      -0.007209  \nstd        1.044718  \nmin       -6.098192  \n25%       -0.440706  \n50%        0.136849  \n75%        0.685555  \nmax        2.649768  \n\n[8 rows x 29 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>SaleID</th>\n      <th>name</th>\n      <th>regDate</th>\n      <th>model</th>\n      <th>brand</th>\n      <th>bodyType</th>\n      <th>fuelType</th>\n      <th>gearbox</th>\n      <th>power</th>\n      <th>kilometer</th>\n      <th>...</th>\n      <th>v_5</th>\n      <th>v_6</th>\n      <th>v_7</th>\n      <th>v_8</th>\n      <th>v_9</th>\n      <th>v_10</th>\n      <th>v_11</th>\n      <th>v_12</th>\n      <th>v_13</th>\n      <th>v_14</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>5.000000e+04</td>\n      <td>50000.00000</td>\n      <td>50000.000000</td>\n      <td>48496.000000</td>\n      <td>47076.000000</td>\n      <td>48032.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>...</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n      <td>50000.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>224999.500000</td>\n      <td>68505.606100</td>\n      <td>2.003401e+07</td>\n      <td>47.64948</td>\n      <td>8.087140</td>\n      <td>1.793736</td>\n      <td>0.376498</td>\n      <td>0.226953</td>\n      <td>119.766960</td>\n      <td>12.598260</td>\n      <td>...</td>\n      <td>0.248147</td>\n      <td>0.044624</td>\n      <td>0.124693</td>\n      <td>0.058198</td>\n      <td>0.062113</td>\n      <td>0.019633</td>\n      <td>0.002759</td>\n      <td>0.004342</td>\n      <td>0.004570</td>\n      <td>-0.007209</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>14433.901067</td>\n      <td>61032.124271</td>\n      <td>5.351615e+04</td>\n      <td>49.90741</td>\n      <td>7.899648</td>\n      <td>1.764970</td>\n      <td>0.549281</td>\n      <td>0.418866</td>\n      <td>206.313348</td>\n      <td>3.912519</td>\n      <td>...</td>\n      <td>0.045836</td>\n      <td>0.051664</td>\n      <td>0.201440</td>\n      <td>0.029171</td>\n      <td>0.035723</td>\n      <td>3.764095</td>\n      <td>3.289523</td>\n      <td>2.515912</td>\n      <td>1.287194</td>\n      <td>1.044718</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>200000.000000</td>\n      <td>1.000000</td>\n      <td>1.991000e+07</td>\n      <td>0.00000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.500000</td>\n      <td>...</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>-9.119719</td>\n      <td>-5.662163</td>\n      <td>-8.291868</td>\n      <td>-4.157649</td>\n      <td>-6.098192</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>212499.750000</td>\n      <td>11315.000000</td>\n      <td>1.999100e+07</td>\n      <td>11.00000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>75.000000</td>\n      <td>12.500000</td>\n      <td>...</td>\n      <td>0.243436</td>\n      <td>0.000035</td>\n      <td>0.062519</td>\n      <td>0.035413</td>\n      <td>0.033880</td>\n      <td>-3.675196</td>\n      <td>-1.963928</td>\n      <td>-1.865406</td>\n      <td>-1.048722</td>\n      <td>-0.440706</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>224999.500000</td>\n      <td>52215.000000</td>\n      <td>2.003091e+07</td>\n      <td>30.00000</td>\n      <td>6.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>0.000000</td>\n      <td>110.000000</td>\n      <td>15.000000</td>\n      <td>...</td>\n      <td>0.257818</td>\n      <td>0.000801</td>\n      <td>0.095880</td>\n      <td>0.056804</td>\n      <td>0.058749</td>\n      <td>1.632134</td>\n      <td>-0.375537</td>\n      <td>-0.138943</td>\n      <td>-0.036352</td>\n      <td>0.136849</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>237499.250000</td>\n      <td>118710.750000</td>\n      <td>2.007110e+07</td>\n      <td>66.00000</td>\n      <td>13.000000</td>\n      <td>3.000000</td>\n      <td>1.000000</td>\n      <td>0.000000</td>\n      <td>150.000000</td>\n      <td>15.000000</td>\n      <td>...</td>\n      <td>0.265263</td>\n      <td>0.101654</td>\n      <td>0.125470</td>\n      <td>0.079387</td>\n      <td>0.087624</td>\n      <td>2.846205</td>\n      <td>1.263451</td>\n      <td>1.775632</td>\n      <td>0.945239</td>\n      <td>0.685555</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>249999.000000</td>\n      <td>196808.000000</td>\n      <td>2.015121e+07</td>\n      <td>246.00000</td>\n      <td>39.000000</td>\n      <td>7.000000</td>\n      <td>6.000000</td>\n      <td>1.000000</td>\n      <td>19211.000000</td>\n      <td>15.000000</td>\n      <td>...</td>\n      <td>0.291176</td>\n      <td>0.153403</td>\n      <td>1.411559</td>\n      <td>0.157458</td>\n      <td>0.211304</td>\n      <td>12.177864</td>\n      <td>18.789496</td>\n      <td>13.384828</td>\n      <td>5.635374</td>\n      <td>2.649768</td>\n    </tr>\n  </tbody>\n</table>\n<p>8 rows × 29 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_csv.info()  # 返回表的信息概况\n",
    "df_csv.describe()  # 表中数值列对应的主要统计量\n",
    "# df_csv.head() # n默认为5\n",
    "# df_csv.tail()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 特征统计函数\n",
    "\n",
    "可以对Series和DataFrame使用，值得主义的是和numpy一样这些函数都有`axis`参数，懂的都懂😎。而且我还发现这些特征统计函数和numpy中的统计函数名字都几乎一样：\n",
    "\n",
    "numpy中：`max, min, mean, median, std, var, sum, quantile, ...`\n",
    "\n",
    "pandas中：`max, min, mean, median, std, var, sum, quantile, count, idxmax, ...`"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 唯一值函数\n",
    "下面两个叫做唯一值函数\n",
    ">对序列使用unique和nunique可以分别得到其唯一值组成的列表和唯一值的个数：\n",
    "\n",
    "发现这句话的重点没？**对序列使用**！对`Series`使用哦！"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 0,  6,  5,  9, 18, 14, 13,  4, 17, 22, 10, 25, 21, 16, 12, 30, 19,\n        1,  8, 31, 27, 11, 26,  3, 35, 24,  7, 33, 32,  2, 15, 20, 28, 29,\n       38, 37, 34, 36, 23, 39], dtype=int64)"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_csv['brand'].unique()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "40"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_csv['brand'].nunique()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### value_counts()\n",
    "\n",
    "我认为这是唯一值函数中分析最高效的方法，所以我将它单独拿出来。注意阅读文档中提到的重要参数，因为很有用。\n",
    "\n",
    ">特征分为类别特征和数字特征\n",
    "\n",
    "🤔也就是说我可以把columns分为两类，比如下面的brand就属于类别特征，使用`value_counts()`很高效！\n",
    "\n",
    "这个统计方法对观察类别特征更有效:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "(-0.04, 9.75]    0.60694\n(9.75, 19.5]     0.29846\n(19.5, 29.25]    0.07308\n(29.25, 39.0]    0.02152\nName: brand, dtype: float64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df_csv['brand'].value_counts(normalize=True,dropna=False)\n",
    "df_csv['brand'].value_counts(normalize=True,dropna=False,bins=4)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### apply()\n",
    "这是个很先进的方法,因为它支持函数式编程\n",
    "\n",
    "但是在下面的实践中我发现参数`x`传入的是**挨个**传入的参数,注意是挨个!!!✔✔✔\n",
    "\n",
    "当是DataFrame对象.apply()时(如下面的`df_csv[['brand','power']]`),每一个`x`实参数是一个`Series`类型；\n",
    "\n",
    "当是Series对象.apply()时（如下面的`df_csv['brand']`），每一个`x`实参是迭代`series.values`，"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0        0\n",
      "1        6\n",
      "2        5\n",
      "3        9\n",
      "4        6\n",
      "        ..\n",
      "49995    4\n",
      "49996    1\n",
      "49997    4\n",
      "49998    1\n",
      "49999    8\n",
      "Name: brand, Length: 50000, dtype: int64\n",
      "0        101\n",
      "1         73\n",
      "2        120\n",
      "3         58\n",
      "4        116\n",
      "        ... \n",
      "49995    150\n",
      "49996    179\n",
      "49997    147\n",
      "49998    176\n",
      "49999      0\n",
      "Name: power, Length: 50000, dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": "brand    None\npower    None\ndtype: object"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这种方式x是一个个series的value，而不是series哦！！！\n",
    "# df_csv['brand'].apply(lambda x:print(x))\n",
    "\n",
    "# 这种方式x是一个个series对象\n",
    "df_csv[['brand','power']].apply(lambda x:print(x))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 窗口对象\n",
    "注意到一个细节，只能对`Series`对象使用"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "Rolling [window=3,center=False,axis=0]"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = df_csv['power'].rolling(window=3)\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 三类替换函数\n",
    "替换一般情况下都是对`Series`进行的（没有那么多为什么，这算是常识吧）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 映射替换 `replace()`\n",
    "这个函数最常用的地方应该是替换`np.nan`，至于替换成什么应该根据具体场景\n",
    "\n",
    "注意这里有两种构造映射表的方式，Ex1用到的是传入两个列表。\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 逻辑替换 `where()` `mask()`\n",
    "我将逻辑替换提出来是因为我发现一个很有趣的python语法：\n",
    "\n",
    "`df_csv['power']<100`\n",
    "\n",
    "我觉得这里应该还是使用到了魔法方法，具体应该是`__lt__()`，定义了小于好的行为，当`x<y`时，调用`x.__lt__(y)`，这个魔法函数应该是在`list()`中实现的"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "0        False\n1         True\n2        False\n3         True\n4        False\n         ...  \n49995    False\n49996    False\n49997    False\n49998    False\n49999     True\nName: power, Length: 50000, dtype: bool"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_csv['power']<100  # 注意这是一个Series类型"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "数值替换 `round()` `abs()` `clip()`"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 练习"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### Ex1：口袋妖怪数据集\n",
    "实践中的收获：data_df是一个DataFrame对象，`data_df[...]`中传入的可以是`Index`对象，很有趣哦，我觉得这里应该使用到了魔法函数，目测应该是`__getitem()__`或`__setitem()__`\n",
    "\n",
    "#### drop_duplicates()\n",
    "\n",
    "`subset`参数比较重要，当为空时：\n",
    ">By default, it removes duplicate rows based on all columns."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "data_df = pd.read_csv('./data/Pokemon.csv')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 800 entries, 0 to 799\n",
      "Data columns (total 13 columns):\n",
      " #   Column      Non-Null Count  Dtype \n",
      "---  ------      --------------  ----- \n",
      " 0   #           800 non-null    int64 \n",
      " 1   Name        800 non-null    object\n",
      " 2   Type 1      800 non-null    object\n",
      " 3   Type 2      414 non-null    object\n",
      " 4   Total       800 non-null    int64 \n",
      " 5   HP          800 non-null    int64 \n",
      " 6   Attack      800 non-null    int64 \n",
      " 7   Defense     800 non-null    int64 \n",
      " 8   Sp. Atk     800 non-null    int64 \n",
      " 9   Sp. Def     800 non-null    int64 \n",
      " 10  Speed       800 non-null    int64 \n",
      " 11  Generation  800 non-null    int64 \n",
      " 12  Legendary   800 non-null    bool  \n",
      "dtypes: bool(1), int64(9), object(3)\n",
      "memory usage: 75.9+ KB\n"
     ]
    }
   ],
   "source": [
    "# 调用head()和info()并观察方法其实算是数据分析的惯性操作了\n",
    "\n",
    "data_df.info()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "   #                   Name Type 1  Type 2  Total  HP  Attack  Defense  \\\n0  1              Bulbasaur  Grass  Poison    318  45      49       49   \n1  2                Ivysaur  Grass  Poison    405  60      62       63   \n2  3               Venusaur  Grass  Poison    525  80      82       83   \n3  3  VenusaurMega Venusaur  Grass  Poison    625  80     100      123   \n4  4             Charmander   Fire     NaN    309  39      52       43   \n\n   Sp. Atk  Sp. Def  Speed  Generation  Legendary  \n0       65       65     45           1      False  \n1       80       80     60           1      False  \n2      100      100     80           1      False  \n3      122      120     80           1      False  \n4       60       50     65           1      False  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>#</th>\n      <th>Name</th>\n      <th>Type 1</th>\n      <th>Type 2</th>\n      <th>Total</th>\n      <th>HP</th>\n      <th>Attack</th>\n      <th>Defense</th>\n      <th>Sp. Atk</th>\n      <th>Sp. Def</th>\n      <th>Speed</th>\n      <th>Generation</th>\n      <th>Legendary</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>Bulbasaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>318</td>\n      <td>45</td>\n      <td>49</td>\n      <td>49</td>\n      <td>65</td>\n      <td>65</td>\n      <td>45</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>Ivysaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>405</td>\n      <td>60</td>\n      <td>62</td>\n      <td>63</td>\n      <td>80</td>\n      <td>80</td>\n      <td>60</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>Venusaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>525</td>\n      <td>80</td>\n      <td>82</td>\n      <td>83</td>\n      <td>100</td>\n      <td>100</td>\n      <td>80</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>VenusaurMega Venusaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>625</td>\n      <td>80</td>\n      <td>100</td>\n      <td>123</td>\n      <td>122</td>\n      <td>120</td>\n      <td>80</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>Charmander</td>\n      <td>Fire</td>\n      <td>NaN</td>\n      <td>309</td>\n      <td>39</td>\n      <td>52</td>\n      <td>43</td>\n      <td>60</td>\n      <td>50</td>\n      <td>65</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "     sum  Total\n0    318    318\n1    405    405\n2    525    525\n3    625    625\n4    309    309\n..   ...    ...\n795  600    600\n796  700    700\n797  600    600\n798  680    680\n799  600    600\n\n[800 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sum</th>\n      <th>Total</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>318</td>\n      <td>318</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>405</td>\n      <td>405</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>525</td>\n      <td>525</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>625</td>\n      <td>625</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>309</td>\n      <td>309</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>795</th>\n      <td>600</td>\n      <td>600</td>\n    </tr>\n    <tr>\n      <th>796</th>\n      <td>700</td>\n      <td>700</td>\n    </tr>\n    <tr>\n      <th>797</th>\n      <td>600</td>\n      <td>600</td>\n    </tr>\n    <tr>\n      <th>798</th>\n      <td>680</td>\n      <td>680</td>\n    </tr>\n    <tr>\n      <th>799</th>\n      <td>600</td>\n      <td>600</td>\n    </tr>\n  </tbody>\n</table>\n<p>800 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：对HP, Attack, Defense, Sp. Atk, Sp. Def, Speed进行加总，验证是否为Total值。\n",
    "\n",
    "pd.DataFrame(\n",
    "    data={'sum':data_df[data_df.columns[5:11]].sum(axis=1),\n",
    "          \"Total\":data_df['Total']}\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "       #                 Name   Type 1  Type 2  Total   HP  Attack  Defense  \\\n0      1            Bulbasaur    Grass  Poison    318   45      49       49   \n1      2              Ivysaur    Grass  Poison    405   60      62       63   \n2      3             Venusaur    Grass  Poison    525   80      82       83   \n4      4           Charmander     Fire     NaN    309   39      52       43   \n5      5           Charmeleon     Fire     NaN    405   58      64       58   \n..   ...                  ...      ...     ...    ...  ...     ...      ...   \n793  717              Yveltal     Dark  Flying    680  126     131       95   \n794  718     Zygarde50% Forme   Dragon  Ground    600  108     100      121   \n795  719              Diancie     Rock   Fairy    600   50     100      150   \n797  720  HoopaHoopa Confined  Psychic   Ghost    600   80     110       60   \n799  721            Volcanion     Fire   Water    600   80     110      120   \n\n     Sp. Atk  Sp. Def  Speed  Generation  Legendary  \n0         65       65     45           1      False  \n1         80       80     60           1      False  \n2        100      100     80           1      False  \n4         60       50     65           1      False  \n5         80       65     80           1      False  \n..       ...      ...    ...         ...        ...  \n793      131       98     99           6       True  \n794       81       95     95           6       True  \n795      100      150     50           6       True  \n797      150      130     70           6       True  \n799      130       90     70           6       True  \n\n[721 rows x 13 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>#</th>\n      <th>Name</th>\n      <th>Type 1</th>\n      <th>Type 2</th>\n      <th>Total</th>\n      <th>HP</th>\n      <th>Attack</th>\n      <th>Defense</th>\n      <th>Sp. Atk</th>\n      <th>Sp. Def</th>\n      <th>Speed</th>\n      <th>Generation</th>\n      <th>Legendary</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>Bulbasaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>318</td>\n      <td>45</td>\n      <td>49</td>\n      <td>49</td>\n      <td>65</td>\n      <td>65</td>\n      <td>45</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>Ivysaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>405</td>\n      <td>60</td>\n      <td>62</td>\n      <td>63</td>\n      <td>80</td>\n      <td>80</td>\n      <td>60</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>Venusaur</td>\n      <td>Grass</td>\n      <td>Poison</td>\n      <td>525</td>\n      <td>80</td>\n      <td>82</td>\n      <td>83</td>\n      <td>100</td>\n      <td>100</td>\n      <td>80</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>Charmander</td>\n      <td>Fire</td>\n      <td>NaN</td>\n      <td>309</td>\n      <td>39</td>\n      <td>52</td>\n      <td>43</td>\n      <td>60</td>\n      <td>50</td>\n      <td>65</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>5</td>\n      <td>Charmeleon</td>\n      <td>Fire</td>\n      <td>NaN</td>\n      <td>405</td>\n      <td>58</td>\n      <td>64</td>\n      <td>58</td>\n      <td>80</td>\n      <td>65</td>\n      <td>80</td>\n      <td>1</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>793</th>\n      <td>717</td>\n      <td>Yveltal</td>\n      <td>Dark</td>\n      <td>Flying</td>\n      <td>680</td>\n      <td>126</td>\n      <td>131</td>\n      <td>95</td>\n      <td>131</td>\n      <td>98</td>\n      <td>99</td>\n      <td>6</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>794</th>\n      <td>718</td>\n      <td>Zygarde50% Forme</td>\n      <td>Dragon</td>\n      <td>Ground</td>\n      <td>600</td>\n      <td>108</td>\n      <td>100</td>\n      <td>121</td>\n      <td>81</td>\n      <td>95</td>\n      <td>95</td>\n      <td>6</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>795</th>\n      <td>719</td>\n      <td>Diancie</td>\n      <td>Rock</td>\n      <td>Fairy</td>\n      <td>600</td>\n      <td>50</td>\n      <td>100</td>\n      <td>150</td>\n      <td>100</td>\n      <td>150</td>\n      <td>50</td>\n      <td>6</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>797</th>\n      <td>720</td>\n      <td>HoopaHoopa Confined</td>\n      <td>Psychic</td>\n      <td>Ghost</td>\n      <td>600</td>\n      <td>80</td>\n      <td>110</td>\n      <td>60</td>\n      <td>150</td>\n      <td>130</td>\n      <td>70</td>\n      <td>6</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>799</th>\n      <td>721</td>\n      <td>Volcanion</td>\n      <td>Fire</td>\n      <td>Water</td>\n      <td>600</td>\n      <td>80</td>\n      <td>110</td>\n      <td>120</td>\n      <td>130</td>\n      <td>90</td>\n      <td>70</td>\n      <td>6</td>\n      <td>True</td>\n    </tr>\n  </tbody>\n</table>\n<p>721 rows × 13 columns</p>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：对于#重复的妖怪只保留第一条记录\n",
    "\n",
    "data_df.drop_duplicates(subset=['#'])  # 只对'#'去重"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "Water       112\nNormal       98\nGrass        70\nBug          69\nPsychic      57\nFire         52\nRock         44\nElectric     44\nGround       32\nGhost        32\nDragon       32\nDark         31\nPoison       28\nSteel        27\nFighting     27\nIce          24\nFairy        17\nFlying        4\nName: Type 1, dtype: int64"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：第一属性的种类数量\n",
    "data_df = data_df.drop(['Total'],1)\n",
    "data_df['Type 1'].value_counts()  # 能再阴一点吗，空格😒"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['Water', 'Normal', 'Grass'], dtype='object')"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：前三多数量对应的种类\n",
    "\n",
    "# 注意区别下面3种方式，明显可以发现第三种其实是前两种结果的组合\n",
    "data_df['Type 1'].value_counts().sort_values(ascending=False).index[:3]\n",
    "# data_df['Type 1'].value_counts().sort_values(ascending=False).values[:3]\n",
    "# data_df['Type 1'].value_counts().sort_values(ascending=False)[:3]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "Type 1   Type 2  \nWater    Steel       1\nFire     Fighting    1\n         Steel       1\n         Rock        1\n         Psychic     1\n                    ..\nPsychic  Fairy       1\n         Fighting    1\n         Fire        1\n         Flying      1\nBug      Electric    1\nLength: 136, dtype: int64"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：第一属性和第二属性的组合种类\n",
    "\n",
    "data_df.drop_duplicates(subset=['Type 1','Type 2'])[['Type 1','Type 2']].value_counts() #"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Q：取出物攻，超过120的替换为high，不足50的替换为low，否则设为mid\n",
    "\n",
    "看参考答案的过程中发现两个非常有意义的点：\n",
    "- 下面代码中出现了`(s>=50) & (s<=120)`这种代码，如果写成`(s>=50) and (s<=120)`就会报错：\n",
    ">The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().\n",
    "\n",
    " 我在[Python 中 （&，|）和（and，or）之间的区别](https://zhuanlan.zhihu.com/p/265970621)中发现如果比较双方是逻辑变量时`&`和`and`是没有区别的，但是文中**专门提到了**在DataFrame中使用`and`会报错，可以把其当作特列。（BTW，python中没有`&&` `||`）。\n",
    "- 对于参考答案中`mask()`的链式写法，我采用以下方式就会报错，报错的内容我倒是能看懂，也就是说values中有非数值类型(`str`)的数据，无法做大小比较。但是我感觉采用链式编程和我这种拆分的方式其实没多大区别。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "0       low\n1       mid\n2       mid\n3       mid\n4       mid\n       ... \n795     mid\n796    high\n797     mid\n798    high\n799     mid\nName: Attack, Length: 800, dtype: object"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = data_df['Attack']\n",
    "# 下面是链式编程，有点意思\n",
    "s.mask(s>120,'high').mask(s<50,'low').mask((s>=50) & (s<=120),'mid') # 目前倒是不知道为什么要使用&，待解决\n",
    "\n",
    "# 但为什么下面这种就不可以，和上面的不是一个道理吗：\n",
    "# s = s.mask(s>120,'high')\n",
    "# s = s.mask(s<50,'low')\n",
    "# s"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "0        GRASS\n1        GRASS\n2        GRASS\n3        GRASS\n4         FIRE\n        ...   \n795       ROCK\n796       ROCK\n797    PSYCHIC\n798    PSYCHIC\n799       FIRE\nName: Type 1, Length: 800, dtype: object"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：取出第一属性，分别用replace和apply替换所有字母为大写\n",
    "\n",
    "#replace，这种方式好复杂但是很牛逼啊\n",
    "#replace有两种替换方式，这里用到的方式是：两个list来构造映射表\n",
    "L = data_df['Type 1'].unique().tolist()  # 这是在构造映射列表中的x项\n",
    "data_df['Type 1'].replace(L,[str.upper(i) for i in L])  # [str.upr..]这里是在构造映射表的y项"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "0        GRASS\n1        GRASS\n2        GRASS\n3        GRASS\n4         FIRE\n        ...   \n795       ROCK\n796       ROCK\n797    PSYCHIC\n798    PSYCHIC\n799       FIRE\nName: Type 1, Length: 800, dtype: object"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#apply\n",
    "data_df['Type 1'].apply(lambda x:str.upper(x))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "       #                 Name   Type 1  Type 2   HP  Attack  Defense  Sp. Atk  \\\n230  213              Shuckle      Bug    Rock   20      10      230       10   \n121  113              Chansey   Normal     NaN  250       5        5       35   \n261  242              Blissey   Normal     NaN  255      10       10       75   \n333  306    AggronMega Aggron    Steel     NaN   70     140      230       60   \n224  208  SteelixMega Steelix    Steel  Ground   75     125      230       55   \n..   ...                  ...      ...     ...  ...     ...      ...      ...   \n552  493               Arceus   Normal     NaN  120     120      120      120   \n760  690               Skrelp   Poison   Water   50      60       60       60   \n538  481              Mesprit  Psychic     NaN   80     105      105      105   \n547  489               Phione    Water     NaN   80      80       80       80   \n165  151                  Mew  Psychic     NaN  100     100      100      100   \n\n     Sp. Def  Speed  Generation  Legendary  Deviation  \n230      230      5           2      False      215.0  \n121      105     50           1      False      207.5  \n261      135     55           2      False      190.0  \n333       80     50           3      False      155.0  \n224       95     30           2      False      145.0  \n..       ...    ...         ...        ...        ...  \n552      120    120           4       True        0.0  \n760       60     30           6      False        0.0  \n538      105     80           4       True        0.0  \n547       80     80           4      False        0.0  \n165      100    100           1      False        0.0  \n\n[800 rows x 13 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>#</th>\n      <th>Name</th>\n      <th>Type 1</th>\n      <th>Type 2</th>\n      <th>HP</th>\n      <th>Attack</th>\n      <th>Defense</th>\n      <th>Sp. Atk</th>\n      <th>Sp. Def</th>\n      <th>Speed</th>\n      <th>Generation</th>\n      <th>Legendary</th>\n      <th>Deviation</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>230</th>\n      <td>213</td>\n      <td>Shuckle</td>\n      <td>Bug</td>\n      <td>Rock</td>\n      <td>20</td>\n      <td>10</td>\n      <td>230</td>\n      <td>10</td>\n      <td>230</td>\n      <td>5</td>\n      <td>2</td>\n      <td>False</td>\n      <td>215.0</td>\n    </tr>\n    <tr>\n      <th>121</th>\n      <td>113</td>\n      <td>Chansey</td>\n      <td>Normal</td>\n      <td>NaN</td>\n      <td>250</td>\n      <td>5</td>\n      <td>5</td>\n      <td>35</td>\n      <td>105</td>\n      <td>50</td>\n      <td>1</td>\n      <td>False</td>\n      <td>207.5</td>\n    </tr>\n    <tr>\n      <th>261</th>\n      <td>242</td>\n      <td>Blissey</td>\n      <td>Normal</td>\n      <td>NaN</td>\n      <td>255</td>\n      <td>10</td>\n      <td>10</td>\n      <td>75</td>\n      <td>135</td>\n      <td>55</td>\n      <td>2</td>\n      <td>False</td>\n      <td>190.0</td>\n    </tr>\n    <tr>\n      <th>333</th>\n      <td>306</td>\n      <td>AggronMega Aggron</td>\n      <td>Steel</td>\n      <td>NaN</td>\n      <td>70</td>\n      <td>140</td>\n      <td>230</td>\n      <td>60</td>\n      <td>80</td>\n      <td>50</td>\n      <td>3</td>\n      <td>False</td>\n      <td>155.0</td>\n    </tr>\n    <tr>\n      <th>224</th>\n      <td>208</td>\n      <td>SteelixMega Steelix</td>\n      <td>Steel</td>\n      <td>Ground</td>\n      <td>75</td>\n      <td>125</td>\n      <td>230</td>\n      <td>55</td>\n      <td>95</td>\n      <td>30</td>\n      <td>2</td>\n      <td>False</td>\n      <td>145.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>552</th>\n      <td>493</td>\n      <td>Arceus</td>\n      <td>Normal</td>\n      <td>NaN</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>120</td>\n      <td>4</td>\n      <td>True</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>760</th>\n      <td>690</td>\n      <td>Skrelp</td>\n      <td>Poison</td>\n      <td>Water</td>\n      <td>50</td>\n      <td>60</td>\n      <td>60</td>\n      <td>60</td>\n      <td>60</td>\n      <td>30</td>\n      <td>6</td>\n      <td>False</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>538</th>\n      <td>481</td>\n      <td>Mesprit</td>\n      <td>Psychic</td>\n      <td>NaN</td>\n      <td>80</td>\n      <td>105</td>\n      <td>105</td>\n      <td>105</td>\n      <td>105</td>\n      <td>80</td>\n      <td>4</td>\n      <td>True</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>547</th>\n      <td>489</td>\n      <td>Phione</td>\n      <td>Water</td>\n      <td>NaN</td>\n      <td>80</td>\n      <td>80</td>\n      <td>80</td>\n      <td>80</td>\n      <td>80</td>\n      <td>80</td>\n      <td>4</td>\n      <td>False</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>165</th>\n      <td>151</td>\n      <td>Mew</td>\n      <td>Psychic</td>\n      <td>NaN</td>\n      <td>100</td>\n      <td>100</td>\n      <td>100</td>\n      <td>100</td>\n      <td>100</td>\n      <td>100</td>\n      <td>1</td>\n      <td>False</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>800 rows × 13 columns</p>\n</div>"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q：求每个妖怪六项能力的离差，即所有能力中偏离中位数最大的值，添加到df并从大到小排序\n",
    "data_df['Deviation'] = data_df[data_df.columns[4:10]].apply(func=lambda x:(x-x.median()).max(),axis=1)  # 注意这里是计算每个妖怪的能力离差，所以应该按照行来求\n",
    "data_df.sort_values(by='Deviation', ascending=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "0      False\n1      False\n2      False\n3      False\n4       True\n       ...  \n795    False\n796    False\n797    False\n798    False\n799    False\nLength: 800, dtype: bool"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Ex2先暂时不考虑做😂，感觉很难啊"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}